<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon16x16.ico?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="java,maven,">










<meta name="description" content="一、为什么使用maven?1.一般的一个项目就是一个工程当一个项目非常庞大，可以借助maven拆分成一个个工程。 2.项目中使用到的jar包，需要复制粘贴到lib目录下借助maven可以将jar保存在仓库，直接引用就行。不用累赘的一个个导包。 3.jar的下载与使用借助maven可以更加便捷快捷，规范化的使用jar包 4.jar包的版本不一致有风险借助maven所有jar存放在仓库，所有项目使用仓">
<meta name="keywords" content="java,maven">
<meta property="og:type" content="article">
<meta property="og:title" content="maven学习">
<meta property="og:url" content="https://liyafei.top/2019/06/27/maven学习/index.html">
<meta property="og:site_name" content="李亚飞的博客">
<meta property="og:description" content="一、为什么使用maven?1.一般的一个项目就是一个工程当一个项目非常庞大，可以借助maven拆分成一个个工程。 2.项目中使用到的jar包，需要复制粘贴到lib目录下借助maven可以将jar保存在仓库，直接引用就行。不用累赘的一个个导包。 3.jar的下载与使用借助maven可以更加便捷快捷，规范化的使用jar包 4.jar包的版本不一致有风险借助maven所有jar存放在仓库，所有项目使用仓">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-09-27T06:46:37.038Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="maven学习">
<meta name="twitter:description" content="一、为什么使用maven?1.一般的一个项目就是一个工程当一个项目非常庞大，可以借助maven拆分成一个个工程。 2.项目中使用到的jar包，需要复制粘贴到lib目录下借助maven可以将jar保存在仓库，直接引用就行。不用累赘的一个个导包。 3.jar的下载与使用借助maven可以更加便捷快捷，规范化的使用jar包 4.jar包的版本不一致有风险借助maven所有jar存放在仓库，所有项目使用仓">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://liyafei.top/2019/06/27/maven学习/">





  <title>maven学习 | 李亚飞的博客</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?32e0d3c6b1cd79c9d8270086407ca0ca";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">李亚飞的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">——漫漫程序人生路</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://liyafei.top/2019/06/27/maven学习/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Leeyf">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="李亚飞的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">maven学习</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-06-27T14:45:38+08:00">
                2019-06-27
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/JAVA/" itemprop="url" rel="index">
                    <span itemprop="name">JAVA</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  2.1k字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  9分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="一、为什么使用maven"><a href="#一、为什么使用maven" class="headerlink" title="一、为什么使用maven?"></a>一、为什么使用maven?</h1><h3 id="1-一般的一个项目就是一个工程"><a href="#1-一般的一个项目就是一个工程" class="headerlink" title="1.一般的一个项目就是一个工程"></a>1.一般的一个项目就是一个工程</h3><p>当一个项目非常庞大，可以借助maven拆分成一个个工程。</p>
<h3 id="2-项目中使用到的jar包，需要复制粘贴到lib目录下"><a href="#2-项目中使用到的jar包，需要复制粘贴到lib目录下" class="headerlink" title="2.项目中使用到的jar包，需要复制粘贴到lib目录下"></a>2.项目中使用到的jar包，需要复制粘贴到lib目录下</h3><p>借助maven可以将jar保存在仓库，直接引用就行。不用累赘的一个个导包。</p>
<h3 id="3-jar的下载与使用"><a href="#3-jar的下载与使用" class="headerlink" title="3.jar的下载与使用"></a>3.jar的下载与使用</h3><p>借助maven可以更加便捷快捷，规范化的使用jar包</p>
<h3 id="4-jar包的版本不一致有风险"><a href="#4-jar包的版本不一致有风险" class="headerlink" title="4.jar包的版本不一致有风险"></a>4.jar包的版本不一致有风险</h3><p>借助maven所有jar存放在仓库，所有项目使用仓库中的一份jar包</p>
<h3 id="5-jar包中如果依赖其他jar需要手动导入"><a href="#5-jar包中如果依赖其他jar需要手动导入" class="headerlink" title="5. jar包中如果依赖其他jar需要手动导入"></a>5. jar包中如果依赖其他jar需要手动导入</h3><p>借助maven会自动的导入相关依赖包</p>
<h1 id="二、什么是maven"><a href="#二、什么是maven" class="headerlink" title="二、什么是maven?"></a>二、什么是maven?</h1><p><a href="https://maven.apache.org/" target="_blank" rel="noopener">Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.</a><br><strong>是一个软件项目管理和理解工具。基于项目对象模型（POM）的概念，Maven可以从一个中心信息管理项目的构建，报告和文档</strong>。</p>
<h3 id="构建："><a href="#构建：" class="headerlink" title="构建："></a>构建：</h3><ul>
<li>定义：把动态的web工程通过编译得到的编译结果部署到服务器上的过程<ul>
<li>a.编译：java源文件–&gt;编译–&gt;Class字节码文件</li>
<li>b.部署：最终在servlet容器中部署的是编译后的文件</li>
</ul>
</li>
<li>构建的各个环节：<ul>
<li>clean [清理] ：将之前编译的文件删除</li>
<li>compile [编译] ： 将java源文件编译成字节码文件</li>
<li>test [测试] ：自动测试，自动调用junit程序</li>
<li>report [报告] ：测试程序的执行结果</li>
<li>package [打包] ： 将动态web打包成war,将java工程打包成jar</li>
<li>install [安装] ：Maven特定的概念——将打包好的文件复制到仓库的指定的位置</li>
<li>deploy [部署] ：将动态web工程war包复制到servlet容器下，并运行<h1 id="三、maven的安装"><a href="#三、maven的安装" class="headerlink" title="三、maven的安装"></a>三、maven的安装</h1><h3 id="1-安装java环境，配置环境变量。"><a href="#1-安装java环境，配置环境变量。" class="headerlink" title="1.安装java环境，配置环境变量。"></a>1.安装java环境，配置环境变量。</h3><h3 id="2-下载maven-并安装"><a href="#2-下载maven-并安装" class="headerlink" title="2.下载maven,并安装"></a>2.<a href="https://maven.apache.org/download.cgi" target="_blank" rel="noopener">下载maven</a>,并<a href="https://maven.apache.org/install.html" target="_blank" rel="noopener">安装</a></h3><h3 id="3"><a href="#3" class="headerlink" title="3."></a>3.<figure class="highlight plain"><figcaption><span>-v ```查验mvn版本</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"># 四、构建一个maven项目从0到1</span><br><span class="line">### 约定目录结构</span><br><span class="line">根目录:工程名</span><br><span class="line">|——src： 源码</span><br><span class="line">|——|——main ： 存放主程序</span><br><span class="line">|——|——|——java: java源码文件</span><br><span class="line">|——|——|——|——|——com</span><br><span class="line">|——|——|——|——|——|——lee</span><br><span class="line">|——|——|——|——|——|——|——maven</span><br><span class="line">|——|——|——|——|——|——|——|——Hello.java</span><br><span class="line">|——|——|——resource： 存放框架配置文件</span><br><span class="line">|——|——test: 存放测试文件</span><br><span class="line">|——pom.xml: maven的核心配置文件</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">### 按照目录结构搭建项目</span><br><span class="line">**pop.xml文件**</span><br><span class="line">```xml</span><br><span class="line"> &lt;?xml version=&quot;1.0&quot; ?&gt;</span><br><span class="line">&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;</span><br><span class="line">&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;</span><br><span class="line">&lt;groupId&gt;com.lee.maven&lt;/groupId&gt;</span><br><span class="line">&lt;artifactId&gt;Hello&lt;/artifactId&gt;</span><br><span class="line">&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;</span><br><span class="line"></span><br><span class="line">&lt;name&gt;Hello&lt;/name&gt;</span><br><span class="line"></span><br><span class="line">&lt;properties&gt;</span><br><span class="line">        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;</span><br><span class="line"></span><br><span class="line">        &lt;maven.compiler.source&gt;11&lt;/maven.compiler.source&gt;</span><br><span class="line">        &lt;maven.compiler.target&gt;11&lt;/maven.compiler.target&gt;</span><br><span class="line">&lt;/properties&gt;</span><br><span class="line">&lt;dependencies&gt;</span><br><span class="line">    &lt;dependency&gt;</span><br><span class="line">	    &lt;groupId&gt;junit&lt;/groupId&gt;</span><br><span class="line">		&lt;artifactId&gt;junit&lt;/artifactId&gt;</span><br><span class="line">		&lt;version&gt;4.0&lt;/version&gt;</span><br><span class="line">		&lt;scope&gt;test&lt;/scope&gt;</span><br><span class="line">	&lt;/dependency&gt;</span><br><span class="line">&lt;/dependencies&gt;</span><br><span class="line">&lt;/project&gt;</span><br></pre></td></tr></table></figure></h3></li>
</ul>
</li>
</ul>
<h4 id="1-进入-xml目录下，执行"><a href="#1-进入-xml目录下，执行" class="headerlink" title="1.进入.xml目录下，执行 "></a>1.进入.xml目录下，执行 <figure class="highlight plain"><figcaption><span>compile```</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">```linux</span><br><span class="line">leeyf@leeyf-PC:~/java/project/maven-hello$ mvn compile</span><br><span class="line">WARNING: An illegal reflective access operation has occurred</span><br><span class="line">WARNING: Illegal reflective access by com.google.inject.internal.cglib.core.$ReflectUtils$1 (file:/usr/share/maven/lib/guice.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)</span><br><span class="line">WARNING: Please consider reporting this to the maintainers of com.google.inject.internal.cglib.core.$ReflectUtils$1</span><br><span class="line">WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations</span><br><span class="line">WARNING: All illegal access operations will be denied in a future release</span><br><span class="line">[INFO] Scanning for projects...</span><br><span class="line">[INFO] </span><br><span class="line">[INFO] ------------------------&lt; com.lee.maven:Hello &gt;-------------------------</span><br><span class="line">[INFO] Building Hello 0.0.1-SNAPSHOT</span><br><span class="line">[INFO] --------------------------------[ jar ]---------------------------------</span><br><span class="line">[INFO] </span><br><span class="line">[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Hello ---</span><br><span class="line">[INFO] Using &apos;UTF-8&apos; encoding to copy filtered resources.</span><br><span class="line">[INFO] Copying 0 resource</span><br><span class="line">[INFO] </span><br><span class="line">[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ Hello ---</span><br><span class="line">[INFO] Changes detected - recompiling the module!</span><br><span class="line">[INFO] Compiling 1 source file to /home/leeyf/java/project/maven-hello/target/classes</span><br><span class="line">[INFO] ------------------------------------------------------------------------</span><br><span class="line">[INFO] BUILD SUCCESS</span><br><span class="line">[INFO] ------------------------------------------------------------------------</span><br><span class="line">[INFO] Total time:  1.072 s</span><br><span class="line">[INFO] Finished at: 2019-06-29T10:45:31+08:00</span><br><span class="line">[INFO] ------------------------------------------------------------------------</span><br></pre></td></tr></table></figure></h4><p>在pom.xml同级目录下，多了一个target文件夹，里面是编译以后的字节码<br>|——target<br>|——|——classes<br>|——|——|——com<br>|——|——|——|——lee<br>|——|——|——|——|——maven<br>|——|——|——|——|——|——Hello.class<br>|——|——maven-status<br>|——|——|——maven-compiler-plugin<br>|——|——|——|——compile<br>|——|——|——|——|——default-compile<br>|——|——|——|——|——|——createdFiles.lst<br>|——|——|——|——|——|——inputFiles.lst<br>|——|——generated-sources<br>|——|——|——annotations</p>
<h4 id="2-运行"><a href="#2-运行" class="headerlink" title="2.运行"></a>2.运行<figure class="highlight plain"><figcaption><span>test-compile```,target 文件夹下多了一个test-class文件夹</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">```linux</span><br><span class="line">leeyf@leeyf-PC:~/java/project/maven-hello$ ls target/</span><br><span class="line">classes  generated-sources  maven-status  test-classes</span><br></pre></td></tr></table></figure></h4><h4 id="3-运行"><a href="#3-运行" class="headerlink" title="3.运行"></a>3.运行<figure class="highlight plain"><figcaption><span>package``` ,target 文件夹下多了一个jar包</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">```linux</span><br><span class="line">leeyf@leeyf-PC:~/java/project/maven-hello$ ls target/</span><br><span class="line">classes  generated-sources  Hello-0.0.1-SNAPSHOT.jar  maven-archiver  maven-status  test-classes</span><br></pre></td></tr></table></figure></h4><h4 id="4-运行"><a href="#4-运行" class="headerlink" title="4.运行"></a>4.运行<figure class="highlight plain"><figcaption><span>clean ``` target整个被清理掉</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"># 五、仓库&amp;&amp;坐标</span><br><span class="line">- **pom.xml**: **Project Object Model 项目对象模型**。它是maven的核心配置文件，所有的构建的配置都在这里设置。</span><br><span class="line">- **坐标**： 使用三个向量确定仓库内唯一一个maven工程</span><br><span class="line">```xml</span><br><span class="line">	&lt;!-- 1.公司或组织域名倒序+项目名--&gt;</span><br><span class="line">	&lt;groupId&gt;junit&lt;/groupId&gt;</span><br><span class="line">	&lt;!--2.模块名--&gt;</span><br><span class="line">	&lt;artifactId&gt;junit&lt;/artifactId&gt;</span><br><span class="line">	&lt;!--3.版本--&gt;</span><br><span class="line">	&lt;version&gt;4.0&lt;/version&gt;</span><br></pre></td></tr></table></figure></h4><ul>
<li><p><strong>maven工程坐标</strong>：</p>
<ul>
<li><p>本项目坐标 </p>
  <figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.lee.maven<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>Hello<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">version</span>&gt;</span>0.0.1-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>依赖项目坐标</p>
  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;dependency&gt;</span><br><span class="line">   		&lt;groupId&gt;junit&lt;/groupId&gt;</span><br><span class="line">	&lt;artifactId&gt;junit&lt;/artifactId&gt;</span><br><span class="line">	&lt;version&gt;4.0&lt;/version&gt;</span><br><span class="line">	&lt;scope&gt;test&lt;/scope&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br></pre></td></tr></table></figure>

<ul>
<li><strong>maven坐标和仓库对应的映射关系：[groupId][artifactId][version][artifactId]-[version].jar</strong></li>
</ul>
</li>
</ul>
</li>
<li><p><strong>仓库</strong></p>
<ul>
<li>1.本地仓库：.m2</li>
<li>2.远程仓库：<ul>
<li>a. 私服：个人搭建的，常见的搭建工具：nexus</li>
<li>b. 中心仓库<h1 id="六、依赖"><a href="#六、依赖" class="headerlink" title="六、依赖"></a>六、依赖</h1></li>
</ul>
</li>
</ul>
</li>
<li><p>maven解析依赖信息会到本地仓库中查取被依赖的jar包</p>
<ul>
<li>当本地仓库中未找到，会去中心仓库查找maven坐标来获取jar包，然后下载到本地仓库</li>
<li>若中心仓库未查取到依赖的jar包时，编译失败</li>
</ul>
</li>
<li><p>若依赖的jar包是自己OR自己团队开发的maven,需要先<figure class="highlight plain"><figcaption><span>install``` 把所依赖的工程jar包安装到本地maven仓库中</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&gt; 举例：现在我再创建第二个maven工程HelloFriend，其中用到了第一个Hello工程里类的sayHello(String name)方法</span><br><span class="line">&gt; 我们在给HelloFriend项目使用 mvn compile命令进行编译的时候，会提示缺少依赖Hello的jar包。怎么办呢？</span><br><span class="line">&gt; 到第一个maven工程中执行 mvn install后，你再去看一下本地仓库，你会发现有了Hello项目的jar包</span><br><span class="line">&gt; 一旦本地仓库有了依赖的maven工程的jar包后，你再到HelloFriend项目中使用 mvn compile命令的时候，可以成功编译</span><br><span class="line">- 依赖范围：</span><br><span class="line">	```xml</span><br><span class="line">	&lt;scope&gt;compile&lt;/scope&gt;</span><br></pre></td></tr></table></figure></p>
<ul>
<li>compile :默认值，本jar包会一直存在</li>
<li>provided :只有开发、测试阶段使用，目的是不让Servlet容器和你本地仓库的jar包冲突 。如servlet.jar。 </li>
<li>runtime : 只在运行时使用，比如JDBC驱动，适用运行和测试阶段</li>
<li>test : 只在测试时适用，用于编译和测试代码。不随项目发布</li>
<li>system : 类似provided，需要显式提供包含依赖的jar，Maven不会在Repository中查找它。<h1 id="七、生命周期"><a href="#七、生命周期" class="headerlink" title="七、生命周期"></a>七、生命周期</h1><h4 id="Maven-有三套相互独立的生命周期"><a href="#Maven-有三套相互独立的生命周期" class="headerlink" title="Maven 有三套相互独立的生命周期"></a>Maven 有三套相互独立的生命周期</h4></li>
</ul>
</li>
<li><p><strong>Clean LifeCycle</strong> 在进行正式构建前进行的一些清理工作</p>
</li>
<li><p><strong>Default Lifecycle</strong> 构建的核心部分，编译，测试，打包，部署</p>
<blockquote>
<p>1、validate<br>2、generate-sources<br>3、process-sources<br>4、generate-resources<br>5、process-resources     复制并处理资源文件，至目标目录，准备打包<br>6、compile     编译项目的源代码<br>7、process-classes<br>8、generate-test-sources<br>9、process-test-sources<br>10、generate-test-resources<br>11、process-test-resources     复制并处理资源文件，至目标测试目录<br>12、test-compile     编译测试源代码<br>13、process-test-classes<br>14、test     使用合适的单元测试框架运行测试。这些测试代码不会被打包或部署<br>15、prepare-package<br>16、package     接受编译好的代码，打包成可发布的格式，如 JAR<br>17、pre-integration-test<br>18、integration-test<br>19、post-integration-test<br>20、verify<br>21、install     将包安装至本地仓库，以让其它项目依赖。<br>22、deploy     将最终的包复制到远程的仓库，以让其它开发人员与项目共享</p>
</blockquote>
<ul>
<li><strong>不论执行哪个生命周期，一定会经过这个周期。</strong></li>
<li><strong>插件：每个阶段都有插件（plugin），插件的职责就是执行它对应的命令。</strong></li>
</ul>
</li>
<li><p><strong>Site Lifecycle</strong> 生成项目报告、站点、发布站点</p>
<ul>
<li><strong>pre-site</strong> 执行一些需要在生成站点前完成的工作</li>
<li><strong>site</strong> 生成项目的站点文档</li>
<li><strong>post-site</strong> 执行一些需要在生成文档之后的任务，并且给部署做准备</li>
<li><strong>site-deploy</strong> 将生成的站点文档部署到特定服务器上</li>
</ul>
</li>
</ul>
<p>部分内容，网络转载，侵权删～</p>

      
    </div>
    
    
    

    <div>
  	
    	 <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-heart"></i>感谢阅读-------------</div>
    
</div>

        
    </div>
    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechatpay.png" alt="Leeyf 微信支付">
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpg" alt="Leeyf 支付宝">
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/java/" rel="tag"># java</a>
          
            <a href="/tags/maven/" rel="tag"># maven</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/06/27/GitHub快速学习-四/" rel="next" title="GitHub快速学习(四)">
                <i class="fa fa-chevron-left"></i> GitHub快速学习(四)
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/08/01/第一章 Linux开山篇/" rel="prev" title="第一章 Linux开山篇">
                第一章 Linux开山篇 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC80Njg4Ni8yMzM4Nw"></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.png" alt="Leeyf">
            
              <p class="site-author-name" itemprop="name">Leeyf</p>
              <p class="site-description motion-element" itemprop="description">——漫漫程序人生路</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">12</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">4</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/leeyf888" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:lee1042245208@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#一、为什么使用maven"><span class="nav-number">1.</span> <span class="nav-text">一、为什么使用maven?</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-一般的一个项目就是一个工程"><span class="nav-number">1.0.1.</span> <span class="nav-text">1.一般的一个项目就是一个工程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-项目中使用到的jar包，需要复制粘贴到lib目录下"><span class="nav-number">1.0.2.</span> <span class="nav-text">2.项目中使用到的jar包，需要复制粘贴到lib目录下</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-jar的下载与使用"><span class="nav-number">1.0.3.</span> <span class="nav-text">3.jar的下载与使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-jar包的版本不一致有风险"><span class="nav-number">1.0.4.</span> <span class="nav-text">4.jar包的版本不一致有风险</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-jar包中如果依赖其他jar需要手动导入"><span class="nav-number">1.0.5.</span> <span class="nav-text">5. jar包中如果依赖其他jar需要手动导入</span></a></li></ol></li></ol><li class="nav-item nav-level-1"><a class="nav-link" href="#二、什么是maven"><span class="nav-number">2.</span> <span class="nav-text">二、什么是maven?</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#构建："><span class="nav-number">2.0.1.</span> <span class="nav-text">构建：</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#三、maven的安装"><span class="nav-number">3.</span> <span class="nav-text">三、maven的安装</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-安装java环境，配置环境变量。"><span class="nav-number">3.0.1.</span> <span class="nav-text">1.安装java环境，配置环境变量。</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-下载maven-并安装"><span class="nav-number">3.0.2.</span> <span class="nav-text">2.下载maven,并安装</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3"><span class="nav-number">3.0.3.</span> <span class="nav-text">3.-v ```查验mvn版本12345678910111213141516171819202122232425262728293031323334353637383940414243# 四、构建一个maven项目从0到1### 约定目录结构根目录:工程名|——src： 源码|——|——main ： 存放主程序|——|——|——java: java源码文件|——|——|——|——|——com|——|——|——|——|——|——lee|——|——|——|——|——|——|——maven|——|——|——|——|——|——|——|——Hello.java|——|——|——resource： 存放框架配置文件|——|——test: 存放测试文件|——pom.xml: maven的核心配置文件### 按照目录结构搭建项目**pop.xml文件**```xml &lt;?xml version=&quot;1.0&quot; ?&gt;&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;&lt;groupId&gt;com.lee.maven&lt;/groupId&gt;&lt;artifactId&gt;Hello&lt;/artifactId&gt;&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;&lt;name&gt;Hello&lt;/name&gt;&lt;properties&gt;        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;        &lt;maven.compiler.source&gt;11&lt;/maven.compiler.source&gt;        &lt;maven.compiler.target&gt;11&lt;/maven.compiler.target&gt;&lt;/properties&gt;&lt;dependencies&gt;    &lt;dependency&gt;	    &lt;groupId&gt;junit&lt;/groupId&gt;		&lt;artifactId&gt;junit&lt;/artifactId&gt;		&lt;version&gt;4.0&lt;/version&gt;		&lt;scope&gt;test&lt;/scope&gt;	&lt;/dependency&gt;&lt;/dependencies&gt;&lt;/project&gt;</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-进入-xml目录下，执行"><span class="nav-number">3.0.3.1.</span> <span class="nav-text">1.进入.xml目录下，执行 compile```1234567891011121314151617181920212223242526```linuxleeyf@leeyf-PC:~/java/project/maven-hello$ mvn compileWARNING: An illegal reflective access operation has occurredWARNING: Illegal reflective access by com.google.inject.internal.cglib.core.$ReflectUtils$1 (file:/usr/share/maven/lib/guice.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)WARNING: Please consider reporting this to the maintainers of com.google.inject.internal.cglib.core.$ReflectUtils$1WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operationsWARNING: All illegal access operations will be denied in a future release[INFO] Scanning for projects...[INFO] [INFO] ------------------------&lt; com.lee.maven:Hello &gt;-------------------------[INFO] Building Hello 0.0.1-SNAPSHOT[INFO] --------------------------------[ jar ]---------------------------------[INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Hello ---[INFO] Using &#39;UTF-8&#39; encoding to copy filtered resources.[INFO] Copying 0 resource[INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ Hello ---[INFO] Changes detected - recompiling the module![INFO] Compiling 1 source file to /home/leeyf/java/project/maven-hello/target/classes[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time:  1.072 s[INFO] Finished at: 2019-06-29T10:45:31+08:00[INFO] ------------------------------------------------------------------------</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-运行"><span class="nav-number">3.0.3.2.</span> <span class="nav-text">2.运行test-compile```,target 文件夹下多了一个test-class文件夹123```linuxleeyf@leeyf-PC:~/java/project/maven-hello$ ls target/classes  generated-sources  maven-status  test-classes</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-运行"><span class="nav-number">3.0.3.3.</span> <span class="nav-text">3.运行package``` ,target 文件夹下多了一个jar包123```linuxleeyf@leeyf-PC:~/java/project/maven-hello$ ls target/classes  generated-sources  Hello-0.0.1-SNAPSHOT.jar  maven-archiver  maven-status  test-classes</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-运行"><span class="nav-number">3.0.3.4.</span> <span class="nav-text">4.运行clean ``` target整个被清理掉1234567891011# 五、仓库&amp;&amp;坐标- **pom.xml**: **Project Object Model 项目对象模型**。它是maven的核心配置文件，所有的构建的配置都在这里设置。- **坐标**： 使用三个向量确定仓库内唯一一个maven工程```xml	&lt;!-- 1.公司或组织域名倒序+项目名--&gt;	&lt;groupId&gt;junit&lt;/groupId&gt;	&lt;!--2.模块名--&gt;	&lt;artifactId&gt;junit&lt;/artifactId&gt;	&lt;!--3.版本--&gt;	&lt;version&gt;4.0&lt;/version&gt;</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#六、依赖"><span class="nav-number">4.</span> <span class="nav-text">六、依赖</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#七、生命周期"><span class="nav-number">5.</span> <span class="nav-text">七、生命周期</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Maven-有三套相互独立的生命周期"><span class="nav-number">5.0.0.1.</span> <span class="nav-text">Maven 有三套相互独立的生命周期</span></a></li></ol></li></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2018 &mdash; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder"><a href="https://www.liyafei.top/">www.liyafei.top</a> All Rights Reserved  备案号:晋ICP备19005844号-1   联系方式：QQ1042245208 友情链接：<a href="http://www.lichuachua.com" target="_blank" style="color:#DDAA00">李歘歘个人博客</a></span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">13k</span>
  
</div>









        




  <script type="text/javascript">
    (function() {
      var hm = document.createElement("script");
      hm.src = "//tajs.qq.com/stats?sId=66475294";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  












  





  

  

  

  
  

  

  

  

</body>
</html>
